<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\ConfigRequest;
use App\Http\Responses\Response;
use App\Models\Config;
use Illuminate\Http\JsonResponse;

class ConfigController extends Controller
{
    public function list(): JsonResponse
    {
        $configs = Config::all();
        return Response::resource_success($configs);
    }

    public function save(ConfigRequest $request): JsonResponse
    {
        $config = Config::create($request->validated());
        return Response::insert_success($config);
    }

    public function update(ConfigRequest $request, $id): JsonResponse
    {
        if (Config::find($id)->update($request->validated())) {
            return Response::update_success(Config::find($id));
        }
        return Response::update_error();
    }

    public function delete($id): JsonResponse
    {
        $config = Config::find($id);
        Config::destroy($id);
        if (Config::find($id)) {
            return Response::delete_error();
        }
        return Response::delete_success($config);
    }
}
